// pages/OrganForm/index.js
const  orgTypelist = [{
  typename: '组织'
}, {
  typename: '学生会'
}, {
  typename: '社团'
}, {
  typename: '创新创业项目'
}, {
  typename: '其它'
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentindex: 0,
    org_type: '',
    orgdata: {},
    org_label: [],
    orgTypelist: orgTypelist,
    piclist: [],
    picCloudurl: [],
    update:false
  },
  bindPickerChange: function (e) {
    let index = e.detail.value
    this.setData({
      org_type: orgTypelist[index].typename,
    })
  },
  addlabel:function (params) {
    let orgLabel = this.data.org_label
    orgLabel.push('')
    this.setData({
      org_label:orgLabel
    })
  },
  inputLabel:function (e) {
    let index = e.currentTarget.dataset.index
    let orgLabel = this.data.org_label
    orgLabel[index] = e.detail.value
    this.setData({
      org_label:orgLabel
    })
  },
  //上传事件
	upImage(e){
    let that = this
		wx.chooseImage({
		  success (res) {
        const tempFilePaths = res.tempFilePaths
        let picCloudurls = []
		    for(let i =1;i<=tempFilePaths.length;i++){
		    		wx.showLoading({
					  title:'上传中'+i+'/'+tempFilePaths.length,                             
					  mask:true                                    
          })
          let filePath = tempFilePaths[i-1];
          const name = Math.random() * 1000000;
          const cloudPath = '' + name + filePath.match(/\.[^.]+?$/)[0]
			    wx.cloud.uploadFile({
			      cloudPath: 'orgImgs/'+cloudPath,
			      filePath: filePath,
			      name: 'file',
			      formData: {},
			      success (res){
			      wx.hideLoading();
             console.log(res);//这块就接收你后台返回过来的图片数据
             picCloudurls.push(res.fileID)
             console.log(picCloudurls)
             that.setData({
              picCloudurl:picCloudurls
             })
			      }
			    })
        }
        
		  }
		})
	},
  formSubmit:function (e) {
    let data = e.detail.value
    data.org_request = data.org_request.split('\n')
    // 处理标签和图片信息
    data.org_label = this.data.org_label
    data.imgUrls = this.data.picCloudurl
    data.org_desc = Date.now()
    wx.cloud.database().collection('orglist').add({
      data:data,
      success:res=>{
        wx.showToast({
          title: '新增成功',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 如果有opions说明是修改
    if(options){
      this.setData({
        update:true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})